home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d980.lha / APipe / child.asm < prev    next >
Assembly Source File  |  1994-04-04  |  5KB  |  212 lines

  1. ********************************************************************************
  2. *
  3. *  child.asm --- child process entry point for ExecCommand()
  4. *
  5. *  Copyright (C) 1991 by Per Bojsen.  All Rights Reserved.
  6. *
  7. *  Permission is granted to any individual or institution to use, copy,
  8. *  modify, and distribute this software, provided that this complete
  9. *  copyright and permission notice is maintained, intact, in all copies
  10. *  and supporting documentation.
  11. *
  12. *  This software is provided on an "as is" basis without express or
  13. *  implied warranty.
  14. *
  15. *  Main entry point is `__ChildProcess', referred to as
  16. *  `int _ChildProcess(void)' from C.
  17. *
  18. *  $Id: child.asm,v 1.2 91/10/12 14:47:06 bojsen Exp Locker: bojsen $
  19. *
  20. *  $Log:        child.asm,v $
  21. *  Revision 1.2  91/10/12  14:47:06  bojsen
  22. *  Changed name of ChildProcess() to _ChildProcess() to avoid possible
  23. *  collisions with client code.
  24. *
  25. *  Revision 1.1  91/10/12  14:39:53  bojsen
  26. *  Initial revision
  27. *
  28.  
  29.         INCLUDE 'exec/types.i'
  30.         INCLUDE 'exec/libraries.i'
  31.         INCLUDE 'exec/ports.i'
  32.  
  33.         INCLUDE 'dos/dos.i'
  34.         INCLUDE 'dos/dosextens.i'
  35.         INCLUDE 'dos/dostags.i'
  36.  
  37. *
  38. * ChildMsg structure
  39. *
  40. * C equivalent:
  41. *
  42. *   struct ChildMsg
  43. *   {
  44. *     struct Message  ExecMsg;
  45. *     char           *CmdLine;
  46. *     LONG            StackSize;
  47. *     BPTR            PathList;
  48. *     ULONG           Flags;
  49. *     LONG            RC;
  50. *   };
  51. *
  52.  
  53.  STRUCTURE CHM,MN_SIZE
  54.     APTR   CHM_CmdLine   ; command line to execute
  55.     LONG   CHM_StackSize ; use this stacksize for the child
  56.     BPTR   CHM_PathList  ; path to install for child
  57.     ULONG  CHM_Flags     ; various flags
  58.     LONG   CHM_RC        ; child's return code
  59.     LABEL  CHM_Size
  60.  
  61.  BITDEF CHM,PIPE,0
  62.  BITDEF CHM,PATH,1
  63.  
  64. ABSEXECBASE EQU 4
  65. LIBVERSION  EQU 37
  66.  
  67.         SECTION butility.lib_text,CODE
  68.  
  69. *
  70. * Entry point
  71. *
  72. * Register usage:
  73. *
  74. *       A6 Current library
  75. *       A5 Process pointer; process port; Child message pointer
  76. *       A4 CLI pointer
  77. *       A0 scratch
  78. *
  79. *       D4 Old path list of process
  80. *       D2
  81.  
  82.         xdef __ChildProcess
  83.  
  84. _LVOOpenLibrary         EQU     -552
  85. _LVOCloseLibrary        EQU     -414
  86. _LVOFindTask            EQU     -294
  87. _LVOForbid              EQU     -132
  88. _LVOWaitPort            EQU     -384
  89. _LVOGetMsg              EQU     -372
  90. _LVOReplyMsg            EQU     -378
  91. _LVOSystemTagList       EQU     -606
  92. _LVOInput               EQU     -54
  93. _LVORead                EQU     -42
  94.  
  95. __ChildProcess:
  96.         movem.l D1-D4/A1/A4-A6,-(A7)
  97.         move.l ABSEXECBASE.w,A6
  98.  
  99.         sub.l A1,A1
  100.         jsr _LVOFindTask(A6)
  101.         move.l D0,A5
  102.  
  103.         ; Get CLI pointer
  104.  
  105.         move.l pr_CLI(A5),D3
  106.  
  107.         ; Get startup message
  108.  
  109.         lea.l pr_MsgPort(A5),A5
  110.         move.l A5,A0
  111.         jsr _LVOWaitPort(A6)
  112.  
  113.         move.l A5,A0
  114.         jsr _LVOGetMsg(A6)
  115.         move.l D0,A5
  116.  
  117.         ; Open dos.library
  118.  
  119.         lea DOSLibraryName(PC),A1
  120.         move.l #LIBVERSION,D0
  121.         jsr _LVOOpenLibrary(A6)
  122.         tst.l D0
  123.         bne.s PrepSystem
  124.  
  125.         moveq #-1,D0
  126.         move.l D0,CHM_RC(A5)
  127.         bra FinishUp
  128.  
  129. PrepSystem:
  130.         ; Switch path if one was passed in
  131.         move.l CHM_Flags(A5),D1
  132.         btst #CHMB_PATH,D1
  133.         bne.s CheckCLI
  134.  
  135.         move.l #0,D3    ; clear CLI pointer so the path is not patched
  136.  
  137. CheckCLI:
  138.         lsl.l #2,D3     ; convert BPTR to CLI
  139.         move.l D3,A4
  140.         beq.s DoSystem  ; was there a CLI?
  141.  
  142.         move.l CHM_PathList(A5),D1
  143.         move.l cli_CommandDir(A4),D4 ; install the path that was passed
  144.         move.l D1,cli_CommandDir(A4) ; in in the startup message
  145.  
  146. DoSystem:
  147.         ; Build tag list for SystemTagList() on stack
  148.  
  149.         pea TAG_DONE
  150.         move.l CHM_StackSize(A5),-(A7)
  151.         pea NP_StackSize
  152.  
  153.         ; Now call SystemTagList() to run child
  154.  
  155.         move.l A7,D2
  156.         move.l CHM_CmdLine(A5),D1
  157.         move.l D0,A6
  158.         jsr _LVOSystemTagList(A6)
  159.         lea $C(A7),A7
  160.         move.l D0,CHM_RC(A5)
  161.  
  162.         move.l A4,D1    ; did we have a CLI?
  163.         beq.s FlushInput
  164.  
  165.         move.l D4,cli_CommandDir(A4) ; OK, reinstall the old path
  166.  
  167. FlushInput:
  168.         ; Flush stdin if it's a pipe (look for PIPE flag)
  169.  
  170.         move.l CHM_Flags(A5),D0
  171.         btst #CHMB_PIPE,D0 ; D0 is CHM_Flags(A5) at this point
  172.         beq.s CloseDOS
  173.  
  174.         jsr _LVOInput(A6)
  175.         move.l D0,D4
  176.         beq.s CloseDOS
  177.  
  178. BUFFERSZ        EQU     512
  179.         lea -BUFFERSZ(A7),A7
  180.         move.l A7,D2
  181.         move.l #BUFFERSZ,D3
  182.  
  183. FlushIn:
  184.         move.l D4,D1
  185.         jsr _LVORead(A6)
  186.         neg.l D0
  187.         bmi.s FlushIn
  188.  
  189.         lea BUFFERSZ(A7),A7
  190.  
  191. CloseDOS:
  192.         ; Close dos.library
  193.  
  194.         move.l A6,A1
  195.         move.l ABSEXECBASE.w,A6
  196.         jsr _LVOCloseLibrary(A6)
  197.  
  198. FinishUp:
  199.         jsr _LVOForbid(A6) ; Forbid() so the parent doesn't unload us too early
  200.  
  201.         move.l A5,A1
  202.         jsr _LVOReplyMsg(A6)
  203.  
  204.         move.l #0,D0
  205.         movem.l (A7)+,D1-D4/A1/A4-A6
  206.         rts
  207.  
  208. DOSLibraryName
  209.         dc.b 'dos.library',0
  210.  
  211.         end
  212.